// pages/user/pickup/pickup.js
var app = getApp();
var setting = app.globalData.setting;
var request = app.request;
var common = require('../../../utils/common.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: setting.url,
    id: 0,
    addressName: '',
    addressDetail: '未选择当前地址',
    shop_id: 0,
    shopList: [],
    searchList: [],
    shop_select: {},
    longitude: '',
    latitude: '',
    word: '', //搜索词
    openSearchModal: false,
    baseUrl: '/api/user/shop_list', //基地址
    requestUrl: '', //请求的链接
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.setData({
      id: options.id
    })
    // this.getList();
    // this.shop_select();
    this.lalo()
  },
  // 获取当前经纬度
  lalo() {
    let that = this
    wx.getLocation({
      isHighAccuracy: true,
      success(res) {
        //console.log(res)
        that.setData({
          longitude: res.longitude,
          latitude: res.latitude,
        });
        that.getList();
        that.shop_select();
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var address = wx.getStorageSync('address');
    if (address) {
      this.setData({
        addressDetail: address
      })
    }
    this.shop_select();
  },
  // 地图导航
  submit_open: function (e) {
    let data = e.currentTarget.dataset.id
    wx.openLocation({
      latitude: Number(data.latitude), //维度
      longitude: Number(data.longitude), //经度
      name: data.shop_address, //目的地定位名称
      scale: 15, //缩放比例
      address: data.shop_address //导航详细地址
    })
  },
  choseAddress() {
    wx.chooseLocation({
      success: res => {
        if (res.address != '' && res.name != '') {
          wx.setStorageSync('address', res.address)
          this.setData({
            addressName: res.name,
            addressDetail: res.address,
            add: false
          })
        }
      }
    })
  },
  shop_select() {
    var that = this;
    request.get(that.data.url + '/api/user/shop_select', {
      data: {
        longitude: that.data.longitude,
        latitude: that.data.latitude
      },
      success: function (res) {
        that.setData({
          shop_select: res.data.result
        });
        wx.setStorageSync('shop_select', res.data.result)
      }
    });
  },

  getList() {
    var that = this;
    request.get(that.data.url + '/api/user/shop_list', {
      data: {
        longitude: that.data.longitude,
        latitude: that.data.latitude,
        shop_name: ''
      },
      success: function (res) {
        //console.log(res.data.result.data)
        that.setData({
          shopList: res.data.result
        });
        wx.stopPullDownRefresh();
      }
    });
  },

  choseShop(e) {
    var that = this;
    var shop_id = e.currentTarget.dataset.id;
    request.post(that.data.url + '/api/user/chose_shop', {
      data: {
        shop_id: shop_id
      },
      success: function (res) {
        if (res.data.status == 1) {
          wx.showToast({
            title: '选择成功',
          })
          // that.shop_select();
          wx.setStorageSync('shop_select', res.data.data)
          setTimeout(function () {
            wx.navigateBack();
          }, 1000);

        }
      }
    });

  },
  openSearchModal: function () {
    this.setData({
      openSearchModal: true
    });
  },

  closeSearchModal: function () {
    this.setData({
      openSearchModal: false
    });
  },

  /** 提交搜索事件 */
  submitSearch: function (e) {
    this.search(e.detail.value.word);
    this.setData({
      openSearchModal: true
    })
  },

  search: function (word) {
    var that = this;
    if (typeof word != 'string' || common.isNull(word)) {
      return app.showTextWarining('请输入搜索关键词');
    }

    this.setData({
      word: word,
      openSearchModal: true
    })
    request.get(that.data.url + '/api/user/shop_list', {
      data: {
        longitude: that.data.longitude,
        latitude: that.data.latitude,
        shop_name: word
      },
      success: function (res) {
        console.log(res, 15)
        that.setData({
          searchList: res.data.result
        });
        wx.stopPullDownRefresh();
      }
    });

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.requestAddressList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})